package cn.qiyu5522.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.qiyu5522.domain.DTO.RoleAddDTO;
import cn.qiyu5522.domain.DTO.RoleChangeDTO;
import cn.qiyu5522.domain.VO.PageVO;
import cn.qiyu5522.domain.VO.RoleVO;
import cn.qiyu5522.domain.entity.ResponseResult;
import cn.qiyu5522.domain.entity.Role;
import cn.qiyu5522.mapper.RoleMapper;
import cn.qiyu5522.service.RoleMenuService;
import cn.qiyu5522.service.RoleService;
import cn.qiyu5522.service.UserRoleService;
import cn.qiyu5522.utils.BeanCopyUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色信息表 服务实现类
 * </p>
 *
 * @author Tonkey
 * @since 2024-04-22
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private UserRoleService userRoleService;

    @Resource
    private RoleMenuService roleMenuService;


    @Override
    public List<String> selectRoleKeyByUserId(Long id) {
        // 若为管理员登录，则直接返回 admin
//        if(SystemConstants.SUPER_ADMINISTER_ID.equals(id) ){
//            List<String> roleKeys = new ArrayList<>();
//            roleKeys.add("admin");
//            return roleKeys;
//        }
        // 根据用户id 获取 角色id
        List<Long> roleIds = userRoleService.selectRoleIdsByUserId(id);
        if(!ObjectUtil.isNotEmpty(roleIds)){
            return new ArrayList<>(0);
        }
        List<Role> roles = listByIds(roleIds);
        List<String> roleKeys = roles.stream().map(r -> r.getRoleKey()).collect(Collectors.toList());
        return roleKeys;
    }

    @Override
    public ResponseResult getRoleListByCondition(Integer pageNum, Integer pageSize, String roleName, String status) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(status),Role::getStatus,status)
                .like(StringUtils.hasText(roleName),Role::getRoleName,roleName);
        Page<Role> page = new Page<>();
        page(page,queryWrapper);
        List<Role> records = page.getRecords();
        // 转 VO
        List<RoleVO> roleVOS = BeanCopyUtils.copyBeanList(records, RoleVO.class);
        PageVO pageVO = new PageVO(roleVOS, page.getTotal());
        return ResponseResult.okResult(pageVO);
    }

    @Override
    public ResponseResult changeStatus(RoleChangeDTO roleChangeDTO) {
        // 转实体类
        Role role = new Role();
        role.setId(roleChangeDTO.getRoleId());
        role.setStatus(roleChangeDTO.getStatus());
        // 角色停用
        updateById(role);
        return ResponseResult.okResult();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResponseResult addRole(RoleAddDTO roleAddDTO) {
        // 转实体类
        Role role = BeanCopyUtils.copyBean(roleAddDTO, Role.class);
        // 添加角色
        save(role);
        // 角色-菜单 中间表中 添加数据
        List<Long> menuIds = roleAddDTO.getMenuIds();
        roleMenuService.saveBatchByRoleIdMenuIds(role.getId(), menuIds);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getRoleById(Long id) {
        Role role = getById(id);
        RoleVO roleVO = BeanCopyUtils.copyBean(role, RoleVO.class);
        return ResponseResult.okResult(roleVO);
    }

}
